<template>
    <div class="main" :class="{'main-hide-text': shrink}">
        <div v-if = "entry" class="sidebar-menu-con" :style="{width: shrink?'60px':'200px', overflow: shrink ? 'visible' : 'auto'}">
            <shrinkable-menu :shrink="shrink" :menu-list="menuList">
                <div slot="top" class="logo-con" @click="gotoHome">
                    <img v-show="!shrink" src="../../assets/images/logo.png" key="max-logo" />
                    <img v-show="shrink" src="../../assets/images/logo-min.png" key="min-logo" />
                </div>
            </shrinkable-menu>
        </div>
        <div v-if = "entry" class="main-header-con" :style="{paddingLeft: shrink?'60px':'200px'}">
            <div class="main-header">
                <div class="navicon-con">
                    <Button :style="{transform: 'rotateZ(' + (this.shrink ? '-90' : '0') + 'deg)'}" type="text" @click="toggleClick">
                        <Icon type="navicon" size="32"></Icon>
                    </Button>
                </div>
                <div class="header-middle-con">
                    <div class="main-breadcrumb">
                        <breadcrumb-nav :currentPath="currentPath"></breadcrumb-nav>
                    </div>
                </div>
                <div class="header-avator-con">
                    <message-tip v-model="mesCount"></message-tip>
                    <div class="user-dropdown-menu-con">
                        <Row type="flex" justify="end" align="middle" class="user-dropdown-innercon">
                            <Dropdown transfer trigger="click" @on-click="handleClickUserDropdown">
                                <a href="javascript:void(0)">
                                    <span class="main-user-name">{{ userName }}</span>
                                    <Icon type="arrow-down-b"></Icon>
                                </a>
                                <DropdownMenu slot="list">
                                    <DropdownItem name="ownSpace">个人信息</DropdownItem>
                                    <DropdownItem name="changePwd">修改密码</DropdownItem>
                                    <DropdownItem name="loginout" divided>退出登录</DropdownItem>
                                </DropdownMenu>
                            </Dropdown>
                            <Avatar icon="person" style="background: #619fe7;margin-left:10px;"></Avatar>
                        </Row>
                    </div>
                </div>
            </div>
        </div>
        <div class="single-page-con" :style="viewStyle">
            <div class="single-page">
                <router-view></router-view>
            </div>
        </div>
    </div>
</template>
<script>
import Cookies from 'js-cookie';
import shrinkableMenu from './shrinkable-menu/shrinkable-menu.vue';
import breadcrumbNav from './breadcrumb-nav.vue';
import messageTip from './message-tip.vue';
import util from '@/utils/util';

export default {
    components: {
        shrinkableMenu,
        breadcrumbNav,
        messageTip
    },
    data() {
        return {
            shrink: false,
            userName: '',
            entry: true
        };
    },
    computed: {
        viewStyle() {
            let obj = {};
            if (this.entry) {
                obj.left = this.shrink?'60px':'200px';
            } else {
                obj.left = '0px';
                obj.top = '0px';
            }
            return obj;
        },

        menuList() {
            //return util.cloneRemoveListen(this.$store.state.base.menuList);
            return this.$store.state.base.menuList;
        },

        currentPath() {
            return this.$store.state.base.currentPath; // 当前面包屑数组
        },

        mesCount() {
            return this.$store.state.base.messageCount;
        }
    },
    methods: {
        init() {
            this.initLayout();
            this.setCurrentPath(this.$route);
            this.userName = Cookies.get('user');
            //消息
            let messageCount = 3;
            this.messageCount = messageCount.toString();
            this.$store.commit('setMessageCount', 3);
        },
        initLayout() {
            let external = util.queryString('external');
            if (external) {
                this.entry = false;
            } else {
                this.entry = true;
            }
        },
        toggleClick() {
            this.shrink = !this.shrink;
        },
        handleClickUserDropdown(name) {
            if (name == 'loginout') {
                //删除缓存用户信息，store信息
                console.log('loginout');
                this.$router.push({
                    name: 'login'
                });
            }
        },
        gotoHome() {
            this.$router.push({
                name: 'home'
            });
        },
        setCurrentPath(router) {
            var currentPathArr = this.getParentPath(router);
            this.$store.commit('setCurrentPath', currentPathArr);
        },
        getParentPath(router) {
            var arr = [],
                routers = this.$store.state.base.menuList;

            routers.forEach(item => {
                if (item.children.length === 1) {
                    if (item.children[0].name === router.name) {
                        arr.push({
                            title: item.children[0].title,
                            //path: item.children[0].path,
                            name: item.children[0].name
                        });
                    }
                } else {
                    item.children.forEach(child => {
                        if (child.name === router.name) {
                            arr.push({
                                title: item.title,
                                //path: item.path,
                                name: item.name
                            });
                            arr.push({
                                title: child.title,
                                //path: child.path,
                                name: child.name
                            });
                        }
                    });
                }
            });
            return arr;
        }
    },
    watch: {
        '$route'(to) {
            //console.log(108,to);
            this.setCurrentPath(to);
        }
    },
    mounted() {
        this.init();
    }
};
</script>
<style lang="less" scoped>
html,
body {
    width: 100%;
    height: 100%;
    background: #f0f0f0;
    overflow: hidden;
}

.lock-screen-back {
    border-radius: 50%;
    z-index: -1;
    box-shadow: 0 0 0 0 #667aa6 inset;
    position: fixed;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    transition: all 3s;
}

.main {
    position: absolute;
    width: 100%;
    height: 100%;
    .unlock-con {
        width: 0px;
        height: 0px;
        position: absolute;
        left: 50%;
        top: 50%;
        z-index: 11000;
    }
    .sidebar-menu-con {
        height: 100%;
        position: fixed;
        top: 0;
        left: 0;
        z-index: 21;
        transition: width .3s;
    }
    .layout-text {
        display: inline-block;
        white-space: nowrap;
        position: absolute;
    }
    .main-hide-text .layout-text {
        display: none;
    }
    &-content-container {
        position: relative;
    }
    &-header-con {
        box-sizing: border-box;
        position: fixed;
        display: block;
        padding-left: 200px;
        width: 100%;
        height: auto;
        z-index: 20;
        box-shadow: 0 2px 1px 1px rgba(100, 100, 100, .1);
        transition: padding .3s;
    }
    &-breadcrumb {
        padding: 8px 15px 0;
    }
    &-menu-left {
        background: #464c5b;
        height: 100%;
    }
    .tags-con {
        height: 40px;
        z-index: -1;
        overflow: hidden;
        background: #f0f0f0;
        .tags-outer-scroll-con {
            position: relative;
            box-sizing: border-box;
            padding-right: 120px;
            width: 100%;
            height: 100%;
            .tags-inner-scroll-body {
                position: absolute;
                padding: 2px 10px;
                overflow: visible;
                white-space: nowrap;
                transition: left .3s ease;
            }
            .close-all-tag-con {
                position: absolute;
                right: 0;
                top: 0;
                box-sizing: border-box;
                padding-top: 8px;
                text-align: center;
                width: 110px;
                height: 100%;
                background: white;
                box-shadow: -3px 0 15px 3px rgba(0, 0, 0, .1);
                z-index: 10;
            }
        }
    }
    &-header {
        height: 60px;
        background: #fff;
        box-shadow: 0 2px 1px 1px rgba(100, 100, 100, .1);
        position: relative;
        z-index: 11;
        .navicon-con {
            margin: 6px;
            display: inline-block;
        }
        .header-middle-con {
            position: absolute;
            left: 60px;
            top: 0;
            right: 340px;
            bottom: 0;
            padding: 10px;
            overflow: hidden;
        }
        .header-avator-con {
            position: absolute;
            right: 0;
            top: 0;
            height: 100%;
            width: 200px;
            .switch-theme-con {
                display: inline-block;
                width: 40px;
                height: 100%;
            }
            .message-con {
                display: inline-block;
                width: 30px;
                padding: 18px 0;
                text-align: center;
                cursor: pointer;
                i {
                    vertical-align: middle;
                }
            }
            .change-skin {
                font-size: 14px;
                font-weight: 500;
                padding-right: 5px;
            }
            .switch-theme {
                height: 100%;
            }
            .user-dropdown {
                &-menu-con {
                    position: absolute;
                    right: 0;
                    top: 0;
                    width: 150px;
                    height: 100%;
                    .main-user-name {
                        display: inline-block;
                        width: 80px;
                        word-break: keep-all;
                        white-space: nowrap;
                        vertical-align: middle;
                        overflow: hidden;
                        text-overflow: ellipsis;
                        text-align: right;
                    }
                }
                &-innercon {
                    height: 100%;
                    padding-right: 14px;
                }
            }
            .full-screen-btn-con {
                display: inline-block;
                width: 30px;
                padding: 18px 0;
                text-align: center;
                cursor: pointer;
                i {
                    vertical-align: middle;
                }
            }
            .lock-screen-btn-con {
                display: inline-block;
                width: 30px;
                padding: 18px 0;
                text-align: center;
                cursor: pointer;
                i {
                    vertical-align: middle;
                }
            }
        }
    }
    .single-page-con {
        position: absolute;
        top: 60px;
        right: 0;
        bottom: 0;
        overflow: auto;
        background-color: #fff;
        z-index: 1;
        transition: left .3s;
        .single-page {
            margin: 10px;
        }
    }
    &-copy {
        text-align: center;
        padding: 10px 0 20px;
        color: #9ea7b4;
    }
}

.taglist-moving-animation-move {
    transition: transform .3s;
}

.logo-con {
    padding: 8px;
    text-align: center;
    cursor: pointer;
    img {
        height: 44px;
        width: auto;
    }
}

</style>
